//
//  CommonCache.m
//  bluebao
//
//  Created by hebidu on 15/8/8.
//  Copyright (c) 2015年 Boye. All rights reserved.
//

#import "CommonCache.h"


@implementation CommonCache



+(BOOL) AlarmSwitchIsOn{
    
    NSString * key = @"COMMON_ALARM_SWITCH";
    
    NSString * state = [[CacheFacade sharedCache] get:key];
    
    if(state == nil || [state isEqualToString:@"YES"]){
        
        return YES;
        
    }
    
    return NO;
}

+(void)setAlarmSwitch:(BOOL )state{
    NSString * key = @"COMMON_ALARM_SWITCH";
    if (state) {
        [[CacheFacade sharedCache] setObject:@"YES" forKey:key afterSeconds:30*24*3600];
    }else{
        [[CacheFacade sharedCache] setObject:@"NO" forKey:key afterSeconds:30*24*3600];
    }
    
}


+(UserModel * )getUserInfo{
    
    UserModel* user =  [[CacheFacade sharedCache] get:@"USER_INFO"];
    //每次获取后都重新设置，用于刷新缓存时间
    if (user != nil) {
            //如果是自动登录，则刷新用户信息缓存。
            if ([self isAutoLogin]) {
        
                [self setUserInfo:user];
            }
    }
    return user;
}

+(void)setUserInfo:(UserModel *) userinfo{
    
    // 默认缓存时间1个小时
    double cacheTime = HUGE_VALF; // 登录缓存改为无限时间
    // 如果开启自动登录
    if ([self isAutoLogin]) {
        // 缓存时间变为7天
        cacheTime = 3600*24*7;
    }

    [[CacheFacade sharedCache] setObject:userinfo forKey:@"USER_INFO" afterSeconds:cacheTime];
}

+(NSString * )getPlaceholderImageName{
    return @"BANMA_PLACEHOLDER_IMAGE";
}

+(UIImage *) getPlaceholderImage{
    NSString * placeholderStr = [CommonCache getPlaceholderImageName];
    return [UIImage imageNamed:placeholderStr ];
}

+(UIImage *) getAvatarPlaceholderImage{
    
    return [UIImage imageNamed:@"AVATAR_PLACEHOLDER" ];
}

+(BOOL) isAutoLogin{
    
    NSString * isautoLogin = (NSString *) [[CacheFacade sharedCache] get:@"IS_AUTO_LOGIN"];
    
    if ([isautoLogin boolValue]) {
        //如果是YES，则自动刷新
        [CommonCache setAutoLogin:YES];
    }
    
    return [isautoLogin boolValue];
}

+(void) setAutoLogin:(BOOL)isAutoLogin{
    [[CacheFacade sharedCache] setObject: [NSString stringWithFormat:@"%d", isAutoLogin ] forKey:@"IS_AUTO_LOGIN" afterSeconds:24*3600*30];
}

+(void)logout{
    [[CacheFacade sharedCache] setObject:nil forKey:@"USER_INFO"];
}

/**
 *  上一次获取短信验证码，倒计时秒数
    ［注册时］
 */
+(NSString *) getCodeCount{
    
    return [[CacheFacade sharedCache] get:@"LAST_GET_CODE_TIME"];
}

/**
 *
 *  上一次获取短信验证码，倒计时秒数
 ［注册时设置］
 *
 *  @param timeInterval 小于0 时 清除缓存值
 */
+(void) setCodeCount:(NSInteger)timeInterval{
    if (timeInterval < 0) {
        [[CacheFacade sharedCache]  setObject:nil forKey:@"LAST_GET_CODE_TIME"];
        return;
    }
    [[CacheFacade sharedCache] setObject: [NSString stringWithFormat:@"%ld" , (long)timeInterval] forKey:@"LAST_GET_CODE_TIME" afterSeconds:timeInterval+1];
    
}

+(BOOL) shouldAlertWhenUseWWAN{
    NSString * key = @"SHOULD_ALERT_WHEN_USE_WWAN";
    
    if ([[CacheFacade sharedCache] get:key] == nil) {
        
        [[CacheFacade sharedCache] setObject:@"YES" forKey:key afterSeconds:600];
        return YES;
    }
    
    return NO;
}

+(BOOL) shouldAlertWhenNetworkOff{
    NSString * key = @"SHOULD_ALERT_WHEN_NETWORK_OFF";
    
    if ([[CacheFacade sharedCache] get:key] == nil) {
        
        [[CacheFacade sharedCache] setObject:@"YES" forKey:key afterSeconds:600];
        return YES;
    }
    
    return NO;
}

// yes：联网
// no：未联网
+ (void)setNetworkStates:(BOOL)states {
    NSString *key = @"network_states";
    
    [[CacheFacade sharedCache] setObject:@(states) forKey:key afterSeconds:HUGE_VALF];
}
+ (BOOL)isNetworkOff {
    NSString *key = @"network_states";
    NSString * state  = [[CacheFacade sharedCache] get:key];
    if(state == nil){
        return NO;
    }
    
    return ![state boolValue];

}

+ (BOOL)needReloadConfig{
    
    NSString * key = @"NEED_RELOAD_CONFIG";
    
    if ([[CacheFacade sharedCache] get:key] == nil) {
        
        //1天
        [[CacheFacade sharedCache] setObject:@"YES" forKey:key afterSeconds:3600*24];
        
        return YES;
    }
    
    return NO;
}




@end
